package com.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.entity.CourseCategory;
 
 
import com.service.CourseCategoryService;
import com.util.Page;
 

@RequestMapping("/product_category")
@Controller
public class CourseCategoryController {
	
	@Autowired
	private  CourseCategoryService  courseCategoryService;
	/*
	 *商品分类页表页 
	 * */
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public ModelAndView list(ModelAndView model){
		model.setViewName("product_category/list");
		return model;
	}
	/*
	 * 查询商品分类页表
	 * */
	@RequestMapping(value="/list",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object>list(@RequestParam(value="name",defaultValue="")String name,
			Page page
			){
		 Map<String,Object> ret=new HashMap<String,Object>();
		 Map<String,Object> queryMap=new HashMap<String,Object>();
		 queryMap.put("name", name);
		 queryMap.put("offset", page.getOffset());
		 queryMap.put("pageSize", page.getRows());
		 ret.put("rows", courseCategoryService.findList(queryMap));
		 ret.put("total",courseCategoryService.getTotal(queryMap) );
		 return ret;
	}
	
	/*
	 * 添加商品分类
	 * */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object>add(CourseCategory courseCategory){
		 Map<String,Object> ret=new HashMap<String,Object>();
	     if(courseCategory==null){
	    	 ret.put("type", "error");
			 ret.put("msg","请填写正确分类信息");
			 return ret;
	     }
	     if(StringUtils.isEmpty(courseCategory.getName())){
	    	 ret.put("type", "error");
			 ret.put("msg","请填写分类名称");
			 return ret;
	     }
	     if(courseCategory.getParentId()!=null&&courseCategory.getParentId().longValue()!=0){
	    	CourseCategory productCategoryParent=courseCategoryService.findById(courseCategory.getParentId());
	    	if(productCategoryParent!=null){
	    		String tags="";
	    		if(productCategoryParent.getTags()!=null){
	    			tags+=productCategoryParent.getTags()+',';
	    		}
	    		courseCategory.setTags( tags+courseCategory.getParentId());
	    	}
	     }
	     if(courseCategoryService.add(courseCategory)<=0){
	    	 ret.put("type", "error");
			 ret.put("msg","添加失败请联系管理员");
			 return ret;
	     }
	     ret.put("type", "success");
		 ret.put("msg","添加成功");
		 return ret;
	}
	/*
	 * 编辑商品分类
	 * */
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object>edit(CourseCategory courseCategory){
		 Map<String,Object> ret=new HashMap<String,Object>();
	     if(courseCategory==null){
	    	 ret.put("type", "error");
			 ret.put("msg","请填写正确分类信息");
			 return ret;
	     }
	     if(StringUtils.isEmpty(courseCategory.getName())){
	    	 ret.put("type", "error");
			 ret.put("msg","请填写分类名称");
			 return ret;
	     }
	     if(courseCategory.getParentId()!=null&&courseCategory.getParentId().longValue()!=0){
	    	CourseCategory productCategoryParent=courseCategoryService.findById(courseCategory.getParentId());
	    	if(productCategoryParent!=null){
	    		String tags="";
	    		if(productCategoryParent.getTags()!=null){
	    			tags+=productCategoryParent.getTags()+',';
	    		}
	    		courseCategory.setTags( tags+courseCategory.getParentId());
	    	}
	     }
	     if(courseCategoryService.edit(courseCategory)<=0){
	    	 ret.put("type", "error");
			 ret.put("msg","编辑失败请联系管理员");
			 return ret;
	     }
	     ret.put("type", "success");
		 ret.put("msg","编辑成功");
		 return ret;
	}
	/*
	 * 删除商品分类
	 * */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object>delete(Long id){
		 Map<String,Object> ret=new HashMap<String,Object>();
	     if(id==null){
	    	 ret.put("type", "error");
			 ret.put("msg","请选择要删除的信息");
			 return ret;
	     }
	     try{
	    	 if(courseCategoryService.delete(id)<=0){
		    	 ret.put("type", "error");
				 ret.put("msg","编辑失败请联系管理员");
				 return ret;
		     }
	     }catch(Exception e){
	    	 ret.put("type", "error");
			 ret.put("msg","该风类存在商品信息，不许删除！");
			 return ret;
	     }
	     
	     ret.put("type", "success");
		 ret.put("msg","删除成功");
		 return ret;
	}
	/**
	 * 返回树形分类
	 * @return
	 */
	@RequestMapping(value="/tree_list",method=RequestMethod.POST)
	@ResponseBody
	public List<Map<String, Object>> treeList(){
		Map<String, Object> queryMap = new HashMap<String, Object>();
		return getTreeCategory(courseCategoryService.findList(queryMap));
	}
	/**
	 * 根据列表生成二级树形关系分类
	 * @param productCategorieList
	 * @return
	 */
	private List<Map<String, Object>> getTreeCategory(List<CourseCategory> productCategorieList){
		List<Map<String, Object>> ret = new ArrayList<Map<String,Object>>();
		//所有的父分类整理
		for(CourseCategory productCategory : productCategorieList){
			if(productCategory.getParentId() == null){
				Map<String, Object> top = new HashMap<String, Object>();
				top.put("id", productCategory.getId());
				top.put("text", productCategory.getName());
				top.put("children", new ArrayList<Map<String,Object>>());
				ret.add(top);
			}
		}
		for(CourseCategory productCategory : productCategorieList){
			if(productCategory.getParentId() != null){
				for(Map<String, Object> map : ret){
					if(productCategory.getParentId().longValue() == Long.valueOf(map.get("id")+"")){
						List children = (List)map.get("children");
						Map<String, Object> child = new HashMap<String, Object>();
						child.put("id", productCategory.getId());
						child.put("text", productCategory.getName());
						children.add(child);
					}
				}
			}
		}
		return ret;
	}
}
 





















